package com.example.pingze.controller;

import com.example.pingze.entity.Compete;
import com.example.pingze.entity.Ranking;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.CompeteService;
import com.example.pingze.service.RankingService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * 全民大战
 */
@RestController
@RequestMapping("/compete")
@CrossOrigin(origins = "*")
public class CompeteController {
    @Autowired
    private CompeteService service;
    @Autowired
    private RankingService rankingService;

    // 存用户发的每一句诗
    @PostMapping("/addCompete")
    public Result addCompete(@RequestBody Compete compete) {

        if (compete == null) {
            throw new PingZeException("compete_竞赛相关信息为空");
        }

        compete.setId(UUID.randomUUID().toString());
        String content = compete.getContent();
        String keyWord = compete.getKeyWord();
        // 通过content和keyword判断数据库中是否有重复数据
        Compete find = service.findCompeteByKeyWordAndContent(keyWord,content);

        if (find == null) {
            Compete add = service.addCompete(compete);
            if (add != null){
                // 获取某一条用户的发表的数据
                Ranking ranking = rankingService.findRankingByAccountAndKeyword(compete.getAccount(),keyWord);
                if (ranking != null) {
                    // 表示排行表中用户有数据,直接修改count数据即可(增加)
                    Ranking updateRanking = rankingService.plusRanking(ranking);
                    if (updateRanking != null) {
                        return ResultVOUtils.success("compete_ranking添加成功");
                    }else {
                        return ResultVOUtils.error("ranking表中数据添加失败，请重新添加");
                    }
                }else {
                    Ranking addRanking = rankingService.addRanking(compete);
                    if (addRanking != null) {
                        return ResultVOUtils.success("compete_ranking添加成功");
                    }else {
                        return ResultVOUtils.error("compete添加成功_ranking添加失败");
                    }
                }
            }else {
                return ResultVOUtils.error("compete_请重新添加");
            }
        } else {
            return ResultVOUtils.success("compete_诗句重复输入，请换一句吧~");
        }
    }
    // 通过关键字获取该主题对应的用户信息
    @PostMapping("/findCompetesByKeyword")
    public Result<List<Compete>> findCompetesByKeyword(@RequestParam String keyWord) {
        if (keyWord == null) {
            throw new PingZeException("compete_关键字为空");
        }
        List<Compete> competes = service.findCompetesByKeyWord(keyWord);
        if (competes == null) {
            throw new PingZeException("compete_该关键字还没有用户发布信息哦，试着评论一条");
        }
        return ResultVOUtils.result(competes);
    }
    // 删除用户评论，本用户只能删除自己的评论
    @PostMapping("/deleteByIdAndAccount")
    public Result deleteByIdAndAccount(@RequestParam String id,
                                       @RequestParam String account) {
        if (id == null) {
            throw new PingZeException("compete_ID为空");
        }
        if (account == null) {
            throw new PingZeException("compete_account为空");
        }
        Compete compete = service.findCompeteByIdAndAccount(id, account);
        if (compete == null) {
            return ResultVOUtils.error("compete_没有找到相关信息");
        }else {
            Ranking ranking = rankingService.findRankingByAccountAndKeyword(compete.getAccount(), compete.getKeyWord());
            if (ranking != null) {
                // 修改ranking表中的count(减)
                Ranking up = rankingService.subRanking(ranking);
                if (up != null){
                    service.deleteCompete(compete);
                    return ResultVOUtils.success("compete_删除成功");
                }else {
                    return ResultVOUtils.error("ranking中的count没有修改成功");
                }
            }else {
                return ResultVOUtils.error("ranking表中的相关数据为空，请检查addCompete接口");
            }
        }
    }

}
